#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Gizmox.WebGUI.Common.Interfaces;

#endregion

namespace TVC.Modules.CustomControl
{
    #region combo box classes
    public partial class MultiColCombo : UserControl
    {
        private class MultiColComboBox : ComboBox
        {
            private MultiColComboForm mobjDropDown = null;
            private string strFilter;
            private string strFind;
            private int[] FilterCol = new int[3];
            private int intDisplayRows = 25;
            private Boolean bitFilter = true;
            private Boolean bitKeyPress = false;
            private BindingSource BScombo;
            private int[] ColWidths;
            private bool[] ColVisible;
            private string[] RowValues;

            public MultiColComboBox()
            {
                this.DropDownStyle = ComboBoxStyle.DropDown;
            }

            protected override Form GetCustomDropDown()
            {
                if (mobjDropDown == null)
                {
                    mobjDropDown = new MultiColComboForm(this);
                    mobjDropDown.Width = Math.Max(this.Width, mobjDropDown.RequiredWidth);
                }
                else if (!mobjDropDown.Refresh)
                {
                    mobjDropDown.Visible = true;
                    mobjDropDown.DGV_Page();
                }
                //mobjDropDown.Load += new EventHandler(mobjDropDown_Load);
                mobjDropDown.Refresh = false;
                return mobjDropDown;
            }

            void mobjDropDown_Load(object sender, EventArgs e)
            {
                //if (this.Context is IApplicationContext)
                //{
                //    ((IApplicationContext)this.Context).SetFocused(this, true);
                //}
            }

           

            protected override bool IsCustomDropDown
            {
                get
                {
                    return true;
                }
            }

            public void Refresh()
            {

                if (mobjDropDown == null)
                {
                    GetCustomDropDown();
                }

                else //if (bitFilter)
                {
                    mobjDropDown.FilterDGV();
                    mobjDropDown.Width = Math.Max(this.Width, mobjDropDown.RequiredWidth);
                }
                mobjDropDown.Refresh = true;
                if (mobjDropDown.Visible == false)
                {
                    mobjDropDown.ShowDropDown();
                }
                if (!bitFilter) mobjDropDown.FindFirst();
                mobjDropDown.Refresh = false;
            }

            public void Close()
            {
                if (mobjDropDown != null)
                {
                    mobjDropDown.Close();
                }
            }

            public void Reset()
            {
                if (mobjDropDown != null)
                {
                    mobjDropDown = null;
                }
            }

            //Data settings
            public BindingSource Binding
            {
                get
                {
                    return BScombo;
                }
                set
                {
                    BScombo = value;
                }
            }

            public string qryFilter
            {
                get
                {
                    return strFilter;
                }
                set
                {
                    strFilter = value;
                }
            }

            public string qryFind
            {
                get
                {
                    return strFind;
                }
                set
                {
                    strFind = value;
                }
            }

            public Boolean Filter
            {
                get
                {
                    return bitFilter;
                }
                set
                {
                    bitFilter = value;
                }
            }

            public Boolean KeyPressChk
            {
                get
                {
                    return bitKeyPress;
                }
                set
                {
                    bitKeyPress = value;
                }
            }

            public int[] ColumnWidths
            {
                get
                {
                    return ColWidths;
                }
                set
                {
                    ColWidths = value;
                }

            }

            public bool[] ColumnVisible
            {
                get
                {
                    return ColVisible;
                }
                set
                {
                    ColVisible = value;
                }

            }

            public string[] RowData
            {
                get
                {
                    return RowValues;
                }
                set
                {
                    RowValues = value;
                }

            }

            public int[] FilterColumn
            {
                get
                {
                    return FilterCol;
                }
                set
                {
                    FilterCol = value;
                }

            }
            public int DisplayRows
            {
                get
                {
                    return intDisplayRows;
                }
                set
                {
                    intDisplayRows = value;
                }
            }

        }

        private class MultiColComboForm : Form
        {
            private MultiColComboBox mobjParent = null;

            private DataGridView objDGV;
            private String strFilter;
            private Boolean bitRefresh = false;
            private BindingSource bsCombo;
            private int x = 0;
            private int y = 1;

            public MultiColComboForm(MultiColComboBox objParent)
            {
                mobjParent = objParent;
                bsCombo = objParent.Binding;
                strFilter = mobjParent.qryFilter;

                this.InitializeComponent();

                SetUpDGV();
                objDGV.AutoGenerateColumns = false;
                this.Width = RequiredWidth;
                ResizeDGV();

            }

            private void MultiColComboForm_Load(object sender, EventArgs e)
            {
                //objDGV.CurrentPage = y;
            }

            public void FilterDGV()
            {
                //Filter the dgv using the text
                x = 0;
                y = 1;
                bsCombo.Filter = mobjParent.qryFilter;
                objDGV.Update();
                ResizeDGV();
            }

            public void FindFirst()
            {
                //Find text in the dgv

                DataView dv = new DataView(((System.Data.DataView)bsCombo.DataSource).ToTable());
                dv.RowFilter = mobjParent.qryFind + " and " + mobjParent.qryFilter;
                dv.Sort = bsCombo.Sort;

                //bsCombo.Position = 0;

                //Get index of first item found
                if (dv.Count > 0)
                {
                    x = bsCombo.Find(objDGV.Columns[0].Name, dv[0][0]);
                    y = x;

                    //set position in binding source
                    bsCombo.Position = x;
                    objDGV.Rows[x].Selected = true;

                    //find the correct page
                    if (x == objDGV.ItemsPerPage)
                    {
                        y = x / objDGV.ItemsPerPage;
                    }
                    else
                    {
                        y = 1 + x / objDGV.ItemsPerPage;
                    }
                    objDGV.CurrentPage = y;

                }
            }

            public void ShowDropDown()
            {
                if (bitRefresh)
                {
                    this.ShowPopup(mobjParent, Gizmox.WebGUI.Forms.DialogAlignment.Below);
                }
            }

            public void SetUpDGV()
            {
                //set up the data table and binding source
                bsCombo.Filter = strFilter;

                // Set up the DataGridView
                objDGV.Dock = DockStyle.Fill;

                // Automatically generate the DataGridView columns.
                objDGV.AutoGenerateColumns = true;
                objDGV.AllowUserToAddRows = false;
                objDGV.AllowUserToDeleteRows = false;

                // Set up the data source.
                objDGV.DataSource = bsCombo;
                objDGV.ItemsPerPage = mobjParent.MaxDropDownItems;

                objDGV.RowHeadersWidthSizeMode = Gizmox.WebGUI.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
                objDGV.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
                objDGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                // Set the DataGridView control's border.
                objDGV.BorderStyle = BorderStyle.Fixed3D;
                objDGV.BackgroundColor = System.Drawing.SystemColors.ControlLightLight;

                objDGV.RowHeadersWidth = 25;

                if (mobjParent.ColumnVisible == null)
                {
                    mobjParent.ColumnVisible = new bool[((System.Data.DataView)bsCombo.DataSource).Table.Columns.Count];
                    for (int i = 0; i < mobjParent.ColumnVisible.Length; i++)
                        mobjParent.ColumnVisible[i] = true;
                }
                if (mobjParent.ColumnWidths == null)
                {
                    mobjParent.ColumnWidths = new int[((System.Data.DataView)bsCombo.DataSource).Table.Columns.Count];
                    for (int i = 0; i < mobjParent.ColumnWidths.Length; i++)
                        mobjParent.ColumnWidths[i] = 100;
                }
                if (mobjParent.RowData == null) 
                mobjParent.RowData = new string[((System.Data.DataView)bsCombo.DataSource).Table.Columns.Count];

            }

            private void objDGV_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex > -1)
                {
                    foreach (DataGridViewColumn objColumn in objDGV.Columns)
                    {
                        mobjParent.RowData[objColumn.Index] = objDGV[objColumn.Index, e.RowIndex].Value.ToString();
                    }
                    mobjParent.DisplayMember = objDGV[mobjParent.FilterColumn[0], e.RowIndex].Value.ToString();
                    mobjParent.ValueMember = objDGV[0, e.RowIndex].Value.ToString();
                    mobjParent.Text = objDGV[mobjParent.FilterColumn[0], e.RowIndex].Value.ToString();

                    y = objDGV.CurrentPage;
                    this.Close();
                }
            }

            private void objDGV_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.RowIndex > -1)
                {

                    foreach (DataGridViewColumn objColumn in objDGV.Columns)
                    {
                        mobjParent.RowData[objColumn.Index] = objDGV[objColumn.Index, e.RowIndex].Value.ToString();
                    }
                    mobjParent.DisplayMember = objDGV[mobjParent.FilterColumn[0], e.RowIndex].Value.ToString();
                    mobjParent.ValueMember = objDGV[0, e.RowIndex].Value.ToString();
                    mobjParent.Text = objDGV[mobjParent.FilterColumn[0], e.RowIndex].Value.ToString();

                    y = objDGV.CurrentPage;
                    this.Close();
                }
            }

            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.Size = new System.Drawing.Size(285, 173);
                objDGV = new DataGridView();
                objDGV.CellClick += new DataGridViewCellEventHandler(objDGV_CellClick);
                objDGV.KeyUp += new KeyEventHandler(objDGV_KeyUp);

                objDGV.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(objDGV_RowHeaderMouseClick);
                this.Controls.Add(objDGV);
                this.Load += new System.EventHandler(this.MultiColComboForm_Load);
                this.ResumeLayout(false);

            }

            void objDGV_KeyUp(object objSender, KeyEventArgs objArgs)
            {
                if (objArgs.KeyCode == Keys.Enter)
                {
                    if (objDGV.CurrentCell == null) return;
                    int indexRow = objDGV.CurrentCell.RowIndex;
                    foreach (DataGridViewColumn objColumn in objDGV.Columns)
                    {
                        if (objColumn.Index >= mobjParent.RowData.Length) break;
                        mobjParent.RowData[objColumn.Index] = objDGV[objColumn.Index, indexRow].Value.ToString();
                    }
                    mobjParent.DisplayMember = objDGV[mobjParent.FilterColumn[0], indexRow].Value.ToString();
                    mobjParent.ValueMember = objDGV[0, indexRow].Value.ToString();
                    mobjParent.Text = objDGV[mobjParent.FilterColumn[0], indexRow].Value.ToString();

                    y = objDGV.CurrentPage;
                    this.Close();
                }
            }



            public void DGV_Page()
            {
                if (objDGV.RowCount > 0)
                {
                    objDGV.CurrentPage = y;
                }
            }

            public void ResizeDGV()
            {
                //Sets the drop down height to fit the data
                if (objDGV.TotalPages > 1 && objDGV.ItemsPerPage <= mobjParent.DisplayRows)
                {
                    this.Height = 45 + (objDGV.Rows[1].Height) * objDGV.ItemsPerPage;
                }
                else if (objDGV.TotalPages > 1 && objDGV.ItemsPerPage > mobjParent.DisplayRows)
                {
                    this.Height = 45 + (objDGV.Rows[1].Height) * mobjParent.DisplayRows;
                }
                else if (objDGV.Rows.GetRowCount(DataGridViewElementStates.None) > mobjParent.DisplayRows)
                {
                    this.Height = 45 + (objDGV.Rows[1].Height) * mobjParent.DisplayRows;
                }
                else if (objDGV.Rows.GetRowCount(DataGridViewElementStates.None) > 0)
                {
                    this.Height = 25 + objDGV.Rows.GetRowCount(DataGridViewElementStates.None) * (objDGV.Rows[0].Height);
                }
                else
                {
                    this.Height = 25;
                }
            }

            public int RequiredWidth
            {
                //Sets the drop down width
                get
                {
                    int intWidth = 45;

                    if (objDGV.Rows.GetRowCount(DataGridViewElementStates.None) <= mobjParent.DisplayRows || mobjParent.MaxDropDownItems <= mobjParent.DisplayRows)
                    {
                        intWidth = 30;
                    }

                    foreach (DataGridViewColumn objColumn in objDGV.Columns)
                    {
                        objDGV.AutoSizeColumnsMode = Gizmox.WebGUI.Forms.DataGridViewAutoSizeColumnsMode.None;
                        objColumn.SortMode = DataGridViewColumnSortMode.Automatic;
                        objColumn.ReadOnly = true;
                        if (mobjParent.ColumnVisible[objColumn.Index])
                        {
                            objColumn.Width = mobjParent.ColumnWidths[objColumn.Index] + 10;
                            intWidth += objColumn.Width;
                        }
                        else
                        {
                            objColumn.Visible = mobjParent.ColumnVisible[objColumn.Index];
                        }

                    }
                    return intWidth;
                }
            }

            public Boolean Refresh
            {
                get
                {
                    return bitRefresh;
                }
                set
                {
                    bitRefresh = value;
                }
            }

            public BindingSource DGV_Binding
            {
                get
                {
                    return bsCombo;
                }
                set
                {
                    bsCombo = value;
                }
            }

        }
    #endregion

        // Delegate declaration
        public delegate void ComboChanged(object sender, ComboChangedEventArgs e);

        public class ComboChangedEventArgs : System.EventArgs
        {
            private string strText;

            public ComboChangedEventArgs(string strText)
            {
                this.strText = strText;
            }

            public string mccbText
            {
                get
                {
                    return this.strText;
                }
            }

        }

        public event ComboChanged ComboText_Changed;

        private String strText = "";
        private String[] strFilterBase = new String[3];
        private Boolean bitSelect = false;
        private string[] ColHdr;
        //private int FilterCol = 1;
        private int intSize;

        public MultiColCombo()
        {
            InitializeComponent();
            textMCCB.Focus();
            //textMCCB.IsAutoComplete = true;

            cmbMCCB.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cmbMCCB.IsAutoComplete = true;
        }


        private void textMCCB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar > 63 && e.KeyChar < 91 || e.KeyChar > 47 && e.KeyChar < 58 || e.KeyChar == 192 || e.KeyChar == 32 || e.KeyChar == 160)
            {
                strText = strText + e.KeyChar.ToString();
            }
            else if (e.KeyChar == 8 && strText.Length > 0)
            {
                strText = strText.Substring(0, strText.Length - 1);
            }
            strText = strText.Replace("''", "'");
            strText = strText.Replace("'", "''");
            strText = strText.Replace((char)160, (char)32);

            if (!Filter)
            {
                cmbMCCB.qryFind = ("[" + ColHdr[cmbMCCB.FilterColumn[0]] + "] like '" + strText + "%'");
                cmbMCCB.Refresh();
                textMCCB.Focus();
            }
            else if (Filter && (e.KeyChar == 13 || cmbMCCB.KeyPressChk))
            {
                strText = strText.ToUpper();
                cmbMCCB.qryFilter = ("[" + ColHdr[cmbMCCB.FilterColumn[0]] + "] like '" + strText + "%' ");

                for (int i = 0; i < strFilterBase.GetUpperBound(0); i++)
                {
                    if (cmbMCCB.FilterColumn[i] != -1)
                    {
                        cmbMCCB.qryFilter = cmbMCCB.qryFilter + (" and [" + ColHdr[cmbMCCB.FilterColumn[i]] + "] " + strFilterBase[i]);
                    }
                }
                cmbMCCB.Refresh();
                textMCCB.Focus();
            }
            else
            {
                cmbMCCB.Close();
            }
            if (bitSelect)
            {
                bitSelect = false;
            }
            textMCCB.Focus();
        }

        private void textMCCB_TextChanged(object sender, EventArgs e)
        {
            strText = textMCCB.Text.Replace("''", "'");
            strText = textMCCB.Text.Replace("'", "''");
            //strText = strText.Replace((char)160, (char)32);

            if (bitSelect)
            {
                bitSelect = false;
            }
           textMCCB.Focus();
        }

        private void cmbMCCB_TextChanged(object sender, EventArgs e)
        {

            if (cmbMCCB.Text != null)
            {
                bitSelect = true;
                textMCCB.Text = cmbMCCB.Text;
                if (ComboText_Changed != null)
                {
                    ComboText_Changed(this, new ComboChangedEventArgs(textMCCB.Text));
                }
            }
            textMCCB.Focus();
        }

        private void cmbMCCB_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cmbMCCB.ValueMember != null)
            {
                bitSelect = true;
                textMCCB.Text = cmbMCCB.DisplayMember;
                if (ComboText_Changed != null)
                {
                    ComboText_Changed(this, new ComboChangedEventArgs(textMCCB.Text));
                }
            }
        }
        private void cmbMCCB_Enter(object sender, EventArgs e)
        {
            if (cmbMCCB.ValueMember != null)
            {
                bitSelect = true;
                textMCCB.Text = cmbMCCB.DisplayMember;
                if (ComboText_Changed != null)
                {
                    ComboText_Changed(this, new ComboChangedEventArgs(textMCCB.Text));
                }
            }
        }

        public BindingSource Binding
        {
            get
            {
                return cmbMCCB.Binding;
            }
            set
            {
                cmbMCCB.Binding = value;
                cmbMCCB.qryFilter = "";
                if (cmbMCCB.Binding != null)
                {

                    if (ColHdr == null) ColHdr = new string[((System.Data.DataView)cmbMCCB.Binding.DataSource).Table.Columns.Count];
                    foreach (DataColumn Col in ((System.Data.DataView)cmbMCCB.Binding.DataSource).Table.Columns)
                    {
                        ColHdr[Col.Ordinal] = Col.ColumnName;
                    }
                    for (int i = 0; i < strFilterBase.GetUpperBound(0); i++)
                    {
                        if (cmbMCCB.FilterColumn[i] != -1)
                        {
                            if (i == 0)
                            {
                                cmbMCCB.qryFilter = ("[" + ColHdr[cmbMCCB.FilterColumn[i]] + "] " + strFilterBase[i]);
                            }
                            else
                            {
                                cmbMCCB.qryFilter = cmbMCCB.qryFilter + (" and [" + ColHdr[cmbMCCB.FilterColumn[i]] + "] " + strFilterBase[i]);
                            }
                        }
                    }

                    cmbMCCB.Binding.Sort = "[" + ColHdr[cmbMCCB.FilterColumn[0]] + "]";
                    cmbMCCB.Reset();
                }

            }
        }

        public int[] ColumnWidths
        {
            set
            {
                cmbMCCB.ColumnWidths = value;
            }
        }

        public bool[] ColumnVisible
        {
            set
            {
                cmbMCCB.ColumnVisible = value;
            }
        }

        public int[] FilterColumn
        {
            get
            {
                return cmbMCCB.FilterColumn;
            }
            set
            {
                cmbMCCB.FilterColumn = value;
            }

        }

        public string[] FilterValue
        {
            get
            {
                return strFilterBase;
            }
            set
            {
                strFilterBase = value;
            }
        }

        public Boolean Filter
        {
            get
            {
                return cmbMCCB.Filter;
            }
            set
            {
                cmbMCCB.Filter = value;
            }
        }

        public Boolean KeyPressChk
        {
            get
            {
                return cmbMCCB.KeyPressChk;
            }
            set
            {
                cmbMCCB.KeyPressChk = value;
            }
        }

        public string[] RowData
        {
            get
            {
                return cmbMCCB.RowData;
            }
            set
            {
                cmbMCCB.Text = null;
                bitSelect = true;
                strText = "";
                textMCCB.Text = null;
                cmbMCCB.RowData = value;
            }

        }

        public int ControlSize
        {
            set
            {
                intSize = value;
                cmbMCCB.Width = intSize - 1;
                textMCCB.Width = intSize - 22;
                this.Update();
            }
        }

        public int MaxDropDownItems
        {
            get
            {
                return cmbMCCB.MaxDropDownItems;
            }
            set
            {
                cmbMCCB.MaxDropDownItems = value;
            }
        }

        public int DisplayRows
        {
            get
            {
                return cmbMCCB.DisplayRows;
            }
            set
            {
                cmbMCCB.DisplayRows = value;
            }
        }

    }

    partial class MultiColCombo
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Visual WebGui UserControl Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textMCCB = new Gizmox.WebGUI.Forms.TextBox();
            this.cmbMCCB = new MultiColComboBox();
            this.SuspendLayout();
            // 
            // textMCCB
            // 
            this.textMCCB.Location = new System.Drawing.Point(0, 0);
            this.textMCCB.Name = "textMCCB";
            this.textMCCB.Size = new System.Drawing.Size(101, 21);
            this.textMCCB.TabIndex = 0;
            this.textMCCB.KeyPress += new KeyPressEventHandler(this.textMCCB_KeyPress);
            //this.textMCCB.TextChanged += new System.EventHandler(this.textMCCB_TextChanged);
            // 
            // cmbMCCB
            // 

            this.cmbMCCB.Binding = null;
            this.cmbMCCB.BorderStyle = Gizmox.WebGUI.Forms.BorderStyle.Fixed3D;
            this.cmbMCCB.Location = new System.Drawing.Point(0, 0);
            this.cmbMCCB.MaxDropDownItems = 8;
            this.cmbMCCB.Name = "cmbMCCB";
            this.cmbMCCB.qryFilter = null;
            this.cmbMCCB.RowData = new string[] { null, null, null, null, null, null, null, null, null, null };
            this.cmbMCCB.Size = new System.Drawing.Size(121, 21);
            this.cmbMCCB.TabIndex = 0;
            //this.cmbMCCB.TextChanged += new System.EventHandler(this.cmbMCCB_TextChanged);
            this.cmbMCCB.SelectedValueChanged += new System.EventHandler(this.cmbMCCB_SelectedValueChanged);
            cmbMCCB.Enter += new System.EventHandler(this.cmbMCCB_Enter);
            // 
            // MCCB
            // 
            this.AutoValidate = Gizmox.WebGUI.Forms.AutoValidate.EnableAllowFocusChange;
            this.BackColor = System.Drawing.Color.Transparent;
            this.Controls.Add(this.textMCCB);
            this.Controls.Add(this.cmbMCCB);
            this.Size = new System.Drawing.Size(122, 21);
            this.Text = "MCCB";
            this.ResumeLayout(false);

        }

        #endregion

        private MultiColComboBox cmbMCCB;
        private Gizmox.WebGUI.Forms.TextBox textMCCB;

    }
}